﻿using AutoMapper;
using PeiYue.Common;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Sys;
using PeiYue.Model.Dto.Sys;
using PeiYue.Model.Models.Sys;
using PeiYue.Services.BASE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Sys
{
    public class DicHeaderService : BaseServices<DicHeaderDao>, IDicHeaderService
    {
        IBaseRepository<DicHeaderDao> _dal;
        IBaseRepository<DicDetailDao> _dicDetaildal;
        IMapper _mapper;
        public DicHeaderService(IBaseRepository<DicHeaderDao> dal,
            IBaseRepository<DicDetailDao> dicDetaildal,
            IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._dicDetaildal = dicDetaildal;
            this._mapper = mapper;
        }
        [Caching(AbsoluteExpiration = 30)]
        public async Task<DicHeaderDto> GetDicHeader(string codec)
        {
            var dicHeaderDao = (await base.Query(t => t.codec == codec && t.status == 1)).FirstOrDefault();
            if (dicHeaderDao == null) return null;
            var headerDto = _mapper.Map<DicHeaderDto>(dicHeaderDao);
            var detailDaoList = await _dicDetaildal.Query(t => t.dic_header_id == dicHeaderDao.id && t.status == 1);
            headerDto.Add(_mapper.Map<List<DicDetailDto>>(detailDaoList));
            return headerDto;
        }
        [Caching(AbsoluteExpiration = 30)]
        public async Task<DicDetailDto> GetDicDetail(string codec, string key)
        {
            var headerDto =await GetDicHeader(codec);
            if (headerDto != null)
            {
                return headerDto.GetDetail(key);
            }
            return null;
        }
        [Caching(AbsoluteExpiration = 30)]
        public async Task<DicDetailDto> GetDicDetailByVal(string codec, string val)
        {
            var headerDto = await GetDicHeader(codec);
            if (headerDto != null)
            {
                return headerDto.GetDetailByVal(val);
            }
            return null;
        }
    }
}
